package com.woniu.woniuproject.web;

import com.woniu.woniuproject.entity.Account_log;
import com.woniu.woniuproject.entity.Debtor_record;
import com.woniu.woniuproject.entity.User;
import com.woniu.woniuproject.exceptions.IlltionExption;
import com.woniu.woniuproject.mapper.AccountMapper;
import com.woniu.woniuproject.mapper.Account_logMapper;
import com.woniu.woniuproject.mapper.moneyCountMapper;
import com.woniu.woniuproject.service.Debtor_recordService;
import com.woniu.woniuproject.service.MonthRepayService;
import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;


@RestController
@RequestMapping("repay")
public class MonthRepayController {
    @Autowired
    MonthRepayService monthRepayService;

    @GetMapping("/plan")
    public Map<String,Double> calc() {

        HashMap<String,Double> map=new HashMap<>();
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUser");
        Integer id = user.getId();
        System.out.println(id);
        List<Debtor_record> list = monthRepayService.selectfordate(id);
        System.out.println(list.toString());
        Calendar instance = Calendar.getInstance(Locale.CHINA);
        int year = instance.get(Calendar.YEAR);
        int year1=year+1;
        Double b1=0.0;Double c1=0.0;Double d1=0.0;double e1=0.0;Double f1=0.0;

        if(list.size()>0) {
            for (Debtor_record a : list) {

                Double b = 0.00;
                Double c = 0.00;
                Double d= 0.00;
                Double e = 0.00;
                Double f= 0.0;
                Date date = a.getReceivabledate();
                Calendar calendar = Calendar.getInstance();
                calendar.setTime(date);
                int i = calendar.get(Calendar.MONTH) + 1;
                int residueperid = 12 - i + 1;
                int currentterm = Integer.parseInt(a.getCurrentterm());
                int totalterm = Integer.parseInt(a.getTotalterm());
                Double principalmoney = a.getPrincipalmoney();
                Double yearrate = a.getYearrate();
                 year= calendar.get(Calendar.YEAR);
                 year1=year+1;


                if (i > currentterm) {
                    b = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) / totalterm * (currentterm - 1);
                    c = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) / totalterm;
                    d = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) * residueperid / totalterm;
                    e = b + d;
                } else if (i + totalterm - currentterm < 12) {
                    b = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) * (i - 1) / totalterm;
                    c = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) / totalterm;
                    d = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) * (totalterm - currentterm + 1) / totalterm;
                    e = b + d;
                } else {
                    e = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) * 12 / totalterm;
                    c = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) / totalterm;
                    b = (totalterm * yearrate / 12 / 100 * principalmoney + principalmoney) * (i - 1) / totalterm;
                    d = e - b;
                }
                f=(totalterm-currentterm-(12-i))*(totalterm * yearrate / 12 / 100 * principalmoney + principalmoney)/totalterm;
                b1 += b;
                c1 += c;
                d1 += d;
                e1 += e;
                f1+=f;


            }
        }


        map.put("b",b1);
            map.put("c",c1);
            map.put("d",d1);
            map.put("e",e1);
            map.put("f",f1);
            map.put("year",Double.parseDouble(String.valueOf(year)));
        map.put("year1",Double.parseDouble(String.valueOf(year1)));
        System.out.println(map);
            return map;



    }

    @Autowired
    moneyCountMapper moneyCountMapper;
    @Autowired
    Account_logMapper account_logMapper;
    @Autowired
            Debtor_recordService debtorRecordService;
    @Autowired
    AccountMapper accountMapper;
    Logger logger = LoggerFactory.getLogger(MonthRepayController.class);
    @RequestMapping("/money")
    @ResponseBody
        public Map<String,String> money(@RequestParam("currentmoney") Double currentmoney) {
        HashMap<String,String> map=new HashMap<>();
       String status="0";
        User user = (User) SecurityUtils.getSubject().getSession().getAttribute("loginUser");

        int id=user.getId();

        System.out.println(id);

        Double balance = accountMapper.selectforbanlance(id);
        if(balance<currentmoney){
            map.put("data",status);
            return map;
        }
        if (moneyCountMapper.updateadd(currentmoney) > 0&&accountMapper.updateforrepaymoney(currentmoney,id)>0) {
            logger.info("本期还款成功");
        } else {
            throw new IlltionExption("本期还款失败");
        }
        //往系统日志添加字段
        Double money = moneyCountMapper.selectmoney();
        Account_log account_log = new Account_log();
        account_log.setAuserid(0);
        account_log.setAmainaccountid(1);
        account_log.setAreceiveorpay("收款");
        account_log.setAdate(new Date());
        account_log.setAtype("贷款");
        account_log.setAamount(currentmoney);
        account_log.setAbeforetradingmoney(money - currentmoney);
        account_log.setAaftertradingmoney(money);
        int o = account_logMapper.sysinsert(account_log);
        if (o == 0) {
            throw new IlltionExption("添加日志失败");
        }
        int i = debtorRecordService.updaterecord(id);
        if(i==0){
            status="2";
            map.put("data",status);
                    return map;
        }
      status="1";
                 map.put("data",status);
        debtorRecordService.deleterecord();

        return map;
    }
}
